package top.conangao.common.holder;

import com.alibaba.ttl.TransmittableThreadLocal;
import lombok.Data;

import java.util.Optional;

/**
 * @author ConanGao
 * @description
 * @since 1.0
 **/
public class UserContextHolder {
    @Data
    public static class User {

        private long id;

        private long tenantId;

        private String deptPath;

        private long deptId;

    }

    private static final ThreadLocal<User> USER_CONTEXT_HOLDER = new TransmittableThreadLocal<>();

    public static void set(User user) {
        USER_CONTEXT_HOLDER.set(user);
    }

    public static User get() {
        return Optional.ofNullable(USER_CONTEXT_HOLDER.get()).orElse(new User());
    }
}
